package com.gkj.blog.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gkj.blog.business.service.TagsService;
import com.gkj.blog.business.vo.entity.TagsVo;
import com.gkj.blog.framework.object.AbstractServiceImpl;
import com.gkj.blog.persistence.mapper.TagsMapper;
import com.gkj.blog.persistence.po.Tags;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

@Service
@CacheConfig(cacheNames = "redis")
public class TagsServiceImpl extends AbstractServiceImpl<TagsVo,Tags> implements TagsService {
    private TagsMapper tagsMapper;

    @Autowired
    public void setTagsMapper(TagsMapper tagsMapper) {
        this.tagsMapper = tagsMapper;
        setBaseMapper(tagsMapper);
    }

    @Override
    @Cacheable(key="'allTags'")
    public List<TagsVo> listAllTags() {
        PageHelper.startPage(1, 100);
        List<Tags> poList = tagsMapper.selectAll();
        List<TagsVo> voList=new LinkedList<>();
        poList.forEach((tag)->{
            voList.add(new TagsVo(tag));
        });
        return voList;
    }

    @Override
    public List<TagsVo> getTags(Long[] tagIds) {
        List<Tags> tags=tagsMapper.getTags(tagIds);
        List<TagsVo> tagVos=new LinkedList<>();
        tags.forEach((e)->{
            tagVos.add(new TagsVo(e));
        });
        return tagVos;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(key = "'allTags'")
    public boolean delete(Long id) {
        return super.delete(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(key = "'allTags'")
    public boolean deleteIds(Long[] ids){
        return super.deleteIds(ids);
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(key = "'allTags'")
    public TagsVo insert(TagsVo record) {
       return super.insert(record);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(key = "'allTags'")
    public boolean update(TagsVo record) {
       return super.update(record);
    }
}
